import {ChangeDetectionStrategy, ChangeDetectorRef, Component, OnInit} from '@angular/core';
import {FormBuilder, FormGroup} from "@angular/forms";
import {fnCheckForm} from "../../../../utils/tools";
import {NzMessageService} from "ng-zorro-antd/message";
import {ValidatorsService} from "../../../../core/services/validators/validators.service";
import {MsdsIdService} from "../../../../core/services/store/biz-store-service/msds-id/msds-id.service";
import {MsdsDangerHttpService} from "../../../../core/services/http/msds/msds-danger.service";
import {AdminService} from "../../../../core/services/store/admin.service";

interface OptionsFaces {
  value: number | string;
  label: string;
}

@Component({
  selector: 'app-msds-danger',
  templateUrl: './msds-danger.component.html',
  styleUrls: ['./msds-danger.component.less'],
  changeDetection: ChangeDetectionStrategy.OnPush
})
export class MsdsDangerComponent implements OnInit {
  validateForm!: FormGroup;
  formOrder = 1;
  msdsEntprId!: number;
  isAdd = false;
  id!: number;


  constructor(private fb: FormBuilder, private msg: NzMessageService,
              private validatorsService: ValidatorsService,
              private cdr: ChangeDetectorRef,
              private msdsIdService: MsdsIdService, public AdminService: AdminService,
              private dataService: MsdsDangerHttpService) {
  }

  initForm(): void {
    this.validateForm = this.fb.group({
      dangerDesc: [null],
      harmfulEnvironment: [null],
      phyCheDanger: [null],
      emergentDesc: [null],
      harmfulHealth: [null],
      gshType: [null],
    });
  }

  submitForm(): void {
    if (!fnCheckForm(this.validateForm)) {
      return;
    }
    const params = this.validateForm.value;
    /*let intoTypeStr = "";
    params.intoType.filter((item: any) => item.checked).forEach((item: { value: string; }) => {
      intoTypeStr += item.value
    });
    params.intoType = intoTypeStr;*/
    if (this.isAdd) {
      params.msdsEntprId = this.msdsEntprId
      this.dataService.addMsdsDanger(params).subscribe(res => {
        this.id = res;
        this.isAdd = false;
      });
    } else {
      params.id = this.id;
      this.dataService.editMsdsDanger(params).subscribe()
    }
  }


  getDetail(msdsEntprId: number): void {
    this.dataService.getMsdsDangerDetail(msdsEntprId).subscribe(res => {
      this.isAdd = !res;
      if (!!res) {
        this.id = res.id as number;
        this.validateForm.patchValue(res);
      }
    })
  }

  ngOnInit(): void {
    this.initForm();
    this.msdsIdService.getMsdsIdStoreStore().subscribe(res => this.msdsEntprId = res);
    this.getDetail(this.msdsEntprId);
  }

}
